package game.entities;

import game.level.Level;
import game.level.tiles.Tile;
import game.InputHandler;

public abstract class Mob extends Entity {

	protected String name;
	protected int speed;
	protected int numSteps = 0;
	protected boolean isMoving;
	protected int movingDir = 1;
	protected int scale = 1;
	protected int velY;
	protected int velX;
	protected int numJumps=0;
	protected int maxJump;
	protected boolean testbool;
	protected boolean testflag=false;

	public Mob(Level level, String name, int x, int y, int speed) {
		super(level);
		this.name = name;
		this.x = x;
		this.y = y;
		this.speed = speed;
	}

	public void move(int xa, int ya, boolean calcGrav, int classType) {
		numSteps++;
		if(classType == 0){
			maxJump = 2;
		}else{
			maxJump = 1;
		}
		if (!hasCollided(xa, ya) && xa != 0 && ya != 0) {
			if (ya < 0) {
				movingDir = 0;
			}
			if (ya > 0) {
				movingDir = 1;
			}
			if (xa < 0) {
				movingDir = 2;
			}
			if (xa > 0) {
				movingDir = 3;
			}
		}
		if (!hasCollided(xa, ya)) {
			if(xa!=0){
				velX += xa;
				if(velX > 2){
					velX = 2;
				}
				if(velX < -2){
					velX = -2;
				}
			}else{
				if(velX > 0){
						velX -= 1;
				}else if (velX < 0){
						velX += 1;
				}
			}
		}
		if(hasCollided(xa,0)){
			velX = 0;
		}
		if (calcGrav) {
			if (isOnGround()) {
				numSteps = 0;
				numJumps = 0;
			}
			if (!isOnGround()) {
				if (numSteps % 7 == 0) {
					velY += 1;
					numSteps -= 5;
				}
			} else if(isOnGround()&&velY>0){
				velY = 0;
			}
			if(hitHead()){
				y+= 1;
				velY = 1;
			}
			if (numJumps < maxJump && ya == -1 && !testbool) {
				velY = -4;
				numJumps++;
			}
			testbool = Player.getHeldJump();
			if (velY > 10) {
				velY = 10;
			}
			int tempy = y;
			for(int x = 0; x<7;x++){
				if (!isSolidTile(0,1,x,7)) {
					if (velY > 0) {
						for (int vel = 0; vel < velY; vel++) {
							y = tempy + vel;
							if (isOnGround()) {
								velY = vel;
								testflag = true;
								break;
							}
						}
						if(testflag){
							break;
						}
					}
				}
			}
			testflag = false;
			y = tempy + velY;
		}
		x += velX;

	}
	public abstract boolean hasCollided(int xa, int ya);

	protected boolean isSolidTile(int xa, int ya, int x, int y) {
		if (level == null)
			return false;
		Tile lastTile = level.getTile((this.x + x) >> 3, (this.y + y) >> 3);
		Tile newTile = level.getTile((this.x + x + xa) >> 3,(this.y + y + ya) >> 3);
		if (!lastTile.equals(newTile) && newTile.isSolid()) {
			return true;
		}
		return false;
	}

	public String getName() {
		return name;
	}

	public int getNumSteps() {
		return numSteps;
	}

	public void setNumSteps(int numSteps) {
		this.numSteps = numSteps;
	}

	public boolean isMoving() {
		return isMoving;
	}

	public void setMoving(boolean isMoving) {
		this.isMoving = isMoving;
	}

	public int getMovingDir() {
		return movingDir;
	}

	public void setMovingDir(int movingDir) {
		this.movingDir = movingDir;
	}

	public boolean isOnGround() {
		int xMin = 1;
		int xMax = 6;
		for (int x = xMin; x < xMax; x++) {
			if (isSolidTile(0,3,5,8)) {
				return true;
			}
		}
		return false;
	}

	public boolean hitHead() {
		int xMin = 0;
		int xMax = 7;
		for (int x = xMin; x < xMax; x++) {
			if (isSolidTile(0,-9,x,3)) {
				return true;
			}
		}
		return false;
	}
}
